package uk.ac.aber.paws.client.core;

import java.util.logging.Level;

import uk.ac.aber.paws.client.constants.Constants;
import uk.ac.aber.paws.client.state.LoginProcessOperations;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 16th August 2011
 * 
 * Operations.java
 * 
 * Stores generic operational procedures such as procedure to performa  clean close down of
 * of this application.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Operations {

	/**
	 * Called when the service is stopped and will attempt to notify the database that the user has logged out.
	 */
	public static void cleanCloseDown(boolean restart) {
		
		// if anybody is logged in, inform the database of a logout
		PAWSClient.SYSTEM_STATE.getLoginRegister().logOutAllUsers();	
			
		String os = PAWSClient.SYSTEM_STATE.getComputerInfo().getOperatingSystem();
		if (os.equals(Constants.OS_FLAG_WINDOWS7) ||
				os.equals(Constants.OS_FLAG_WINDOWSXP) ||
				os.equals(Constants.OS_FLAG_WINDOWSVISTA)) {
			
			Initiator.LOG_FILE.log(Level.INFO,new Boolean(restart).toString());
			
			try {
				
				if (restart) {
					Initiator.LOG_FILE.log(Level.INFO,"Restarting PAWS Client Service");
			
					org.rzo.yajsw.app.WrapperJVMMain.WRAPPER_MANAGER.restart();
				}
				else {
					org.rzo.yajsw.app.WrapperJVMMain.WRAPPER_MANAGER.stop();
				}
			} 
			catch (Exception e) {
				Initiator.LOG_FILE.log(Level.ALL,e.toString());
			} 
			
		}
		else if (PAWSClient.SYSTEM_STATE.getComputerInfo().getOperatingSystem().equals(Constants.OS_FLAG_LINUX)) {
			
			System.exit(1);
		}
		else if (PAWSClient.SYSTEM_STATE.getComputerInfo().getOperatingSystem().equals(Constants.OS_FLAG_MAC)) {
			
			System.exit(1);
		}
	}
}
